package com.rzj.book.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.rzj.book.bo.UserBO;
import com.rzj.book.en.User;
import com.rzj.book.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author Administrator
 * @Date 2020/5/14 20:55
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @ResponseBody
    @RequestMapping("/findAllUser")
    public List<User> findAllUser() {

        List<User> all = null;
        try {
            all = userService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return all;
    }

    @ResponseBody
    @RequestMapping("/getList")
    public void getList() {
        List<User> lists = new ArrayList<User>();
        try {
            lists = userService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONArray array = JSONArray.parseArray(JSON.toJSONString(lists));
        sendJSONArrayGrid(array);
    }

    @ResponseBody
    @RequestMapping("/getlist")
    public void getlist() {
        List<User> lists = new ArrayList<User>();
        try {
            lists = userService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        write(JSON.toJSONString(lists));
    }

    @ResponseBody
    @RequestMapping(value = "/findByPage", method = RequestMethod.POST)
    public void findByPage(UserBO userBO, @RequestParam(value = "page") Integer page, @RequestParam("pageSize") Integer pageSize) {
        List<User> lists = new ArrayList<User>();
        Integer total = 0;
        try {
            total = userService.findByPageCount(userBO, page, pageSize);
            lists = userService.findByPage(userBO, page, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        sendJSONArrayCountGrid(JSONArray.parseArray(JSON.toJSONString(lists)), total);
    }

    @ResponseBody
    @RequestMapping(value = "/getUserById", method = RequestMethod.POST)
    public void getUserById(@RequestParam(value = "id") Integer id) {
        try {
            User user = userService.getUserById(id);
            write(JSON.toJSONString(user));
        } catch (Exception e) {
            ajaxReturn(false, e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public void addUser(User user) {
        try {
            userService.addUser(user);
            ajaxReturn(true, "添加成功");
        } catch (Exception e) {
            ajaxReturn(false, "添加失败");
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public void updateUser(User user) {
        try {
            userService.updateUser(user);
            ajaxReturn(true, "更新成功");
        } catch (Exception e) {
            ajaxReturn(false, "更新失败");
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    public void deleteUser(@RequestParam(value = "id")Integer id) {
        try {
            userService.deleteUser(id);
            ajaxReturn(true, "删除成功");
        } catch (Exception e) {
            ajaxReturn(false, "删除失败");
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/findUserByName", method = RequestMethod.POST)
    public void findUserByName(String userName) throws IOException {
        System.out.println(userName);
        // 调用Service进行查询:
        User existUser = userService.findByUsername(userName);
        if (existUser != null) {
            write("<font color='red'>用户名已经存在</font>");
        } else {
            write("<font color='green'>用户名可以使用</font>");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/insertUserByRegist", method = RequestMethod.POST)
    public void insertUserByRegist(String code,User user) {
        HttpSession session = getSession();
        // 从session中获得验证码的随机值:
        String sessionCode = (String)session.getAttribute("checkcode");
        if(!code.equalsIgnoreCase(sessionCode)){
            ajaxReturn(false, "验证码错误");
            return ;
        }
        userService.addUser(user);
        ajaxReturn(true, "注册成功");
    }

    /**
     * 退出登陆
     */
    @ResponseBody
    @RequestMapping(value = "/loginOut", method = RequestMethod.POST)
    public void loginOut(){
       getSession().invalidate();
    }
}
